#!/usr/bin/bash

#============================================
# 检测Nginx服务（或网站/API）是否正常
# 参考：https://www.imooc.com/video/10514
#============================================

clear
#初始化终端颜色，即清除颜色
resetTerminal=$(tput sgr0)
GREEN='\e[1;32m' #绿
RED='\e[1;31m' #红
nginxServer="http://entertest.morvoo.com/api/v1/index"

function checkNginxServer() {
    statusCode=$(curl -m 5 -s -w %{http_code} ${nginxServer} -o /dev/null)
    if [ $statusCode -eq 000 -o $statusCode -ge 500 ]; then
        echo -e "${RED}Nginx连接失败! Response status code: ${resetTerminal}" $statusCode
    else
        httpContent=$(curl -s ${nginxServer})
        echo -e "${GREEN}Nginx连接成功! ${resetTerminal} $httpContent"
    fi
}

checkNginxServer
